/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.managedbean;

import au.edu.swin.swinvehicle.driver.DriverDetailedTO;
import au.edu.swin.swinvehicle.driver.DriverManagerException;
import au.edu.swin.swinvehicle.driver.DriverManagerRemote;
import au.edu.swin.swinvehicle.driverlicencetype.DriverLicenceTypeManagerException;
import au.edu.swin.swinvehicle.driverlicencetype.DriverLicenceTypeManagerRemote;
import au.edu.swin.swinvehicle.driverlicencetype.DriverLicenceTypeTO;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author Administrator
 */
public class DriverJSFManagedBean {

    public static final String LIST_DRIVERS = "listDrivers";
    public static final String ADD_DRIVER = "addDriver";
    public static final String EDIT_DRIVER = "editDriver";
    @EJB
    private DriverManagerRemote driverManagerBean;
    @EJB
    private DriverLicenceTypeManagerRemote driverLicenceTypeManagerBean;
    private DriverDetailedTO driver;

    /** Creates a new instance of DriverJSFManagedBean */
    public DriverJSFManagedBean() {
        this.driver = new DriverDetailedTO();
        this.driver.setDriverLicenceType(new DriverLicenceTypeTO());
    }

    public DriverDetailedTO getDriver() {
        return driver;
    }

    public void setDriver(DriverDetailedTO driver) {
        this.driver = driver;
    }

    public List<DriverDetailedTO> getDrivers() {
        List<DriverDetailedTO> drivers = new ArrayList<DriverDetailedTO>();
        try {
            drivers = this.driverManagerBean.getAllDetailedDrivers();
        } catch (DriverManagerException ex) {
            Logger.getLogger(DriverJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return drivers;
    }

    public String addDriver() {
        try {
            // Check if username is available
            if (this.driverManagerBean.isUsernameAvailable(this.driver.getUsername())) {
                this.driverManagerBean.addDriver(this.driver);
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_INFO);
                msg.setSummary("Added:");
                msg.setDetail("The new driver has been successfully added");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralDriverSuccessNotification", msg);
                return LIST_DRIVERS;
            } else {
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Invalid username:");
                msg.setDetail("The username is not available. Please try again.");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralDriverErrorNotification", msg);
                return ADD_DRIVER;
            }
        } catch (DriverManagerException ex) {
            Logger.getLogger(DriverJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot add new driver. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralDriverErrorNotification", msg);
            return ADD_DRIVER;
        }
    }

    public List<SelectItem> getDriverLicenceTypes() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "--- Select driver licence type ---"));
        try {
            List<DriverLicenceTypeTO> driverLicenceTypes = this.driverLicenceTypeManagerBean.getAllDriverLicenceTypes();
            for (DriverLicenceTypeTO driverLicenceType : driverLicenceTypes) {
                selectItems.add(new SelectItem(driverLicenceType.getId(), driverLicenceType.getCode()));
            }
        } catch (DriverLicenceTypeManagerException ex) {
            Logger.getLogger(DriverJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return selectItems;
    }

    public String editDriver() {
        String driverIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("driverId");
        try {
            Long driverId = Long.parseLong(driverIdString);
            this.driver = this.driverManagerBean.getDriver(driverId);
            if (this.driver == null) {
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Invalid driver ID:");
                msg.setDetail("The driver does not exist. Please try again.");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralDriverErrorNotification", msg);
                return LIST_DRIVERS;
            } else {
                return EDIT_DRIVER;
            }
        } catch (DriverManagerException ex) {
            Logger.getLogger(DriverJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot edit driver. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralDriverErrorNotification", msg);

            return LIST_DRIVERS;
        }
    }

    public String saveDriver() {
        try {
            // Check if the new username is available
            if (this.driverManagerBean.getDriver(this.driver.getId()).getUsername().equals(this.driver.getUsername()) || this.driverManagerBean.isUsernameAvailable(this.driver.getUsername())) {
                this.driverManagerBean.editDriver(this.driver);
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_INFO);
                msg.setSummary("Updated:");
                msg.setDetail("The driver has been successfully updated");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralDriverSuccessNotification", msg);

                return LIST_DRIVERS;
            } else {
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Invalid username:");
                msg.setDetail("The username is not available. Please try again.");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralDriverErrorNotification", msg);

                return EDIT_DRIVER;
            }
        } catch (DriverManagerException ex) {
            Logger.getLogger(DriverJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            Logger.getLogger(DriverJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot edit driver. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralDriverErrorNotification", msg);
            return EDIT_DRIVER;
        }
    }

    public String deleteDriver() {
        String driverIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("driverId");
        try {
            Long driverId = Long.parseLong(driverIdString);
            this.driverManagerBean.deleteDriver(driverId);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            msg.setSummary("Deleted:");
            msg.setDetail("The driver has been successfully deleted");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralDriverSuccessNotification", msg);

            return LIST_DRIVERS;
        } catch (DriverManagerException ex) {
            Logger.getLogger(DriverJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot delete driver. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralDriverErrorNotification", msg);

            return LIST_DRIVERS;
        }
    }
}
